package admin;

import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

import model.*;
import util.*;


public class AdminAction extends Action {
	
	public ActionForward execute(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
 
		
		//Form취득
		AdminForm adminform = (AdminForm)form;
		
		
		int totalRows;
		int holdCommCount;
		Pager pager = new Pager();
		MemberDAO memberDao = new MemberDAO();
		CommunityDAO commDao = new CommunityDAO();
		
		totalRows = memberDao.getTotalRows();
		pager.init(req, totalRows, 3, 5);
		Vector<Member> list = null;
		list = memberDao.getBoardListPage(pager.getCurrentPage(), pager.getRowsPerPage());
		adminform.setList(list);
		
		holdCommCount = commDao.getHoldonCommunityCount();
		adminform.setHoldCommCount(holdCommCount);
		
		long allBoardCount;	//전체게시판
		long noticeCount;	//공지사항
		long qnaCount;		//Q&A게시판
		long freeCount;		//자유게시판
		long dataCount;		//자료게시판
		long photoCount;	//앨범게시판
		
		NoticeBoardDAO noticeDao = new NoticeBoardDAO();	//notice DAO
		QnaBoardDAO qnaDao = new QnaBoardDAO();				//qna DAO
		FreeBoardDAO freeDao = new FreeBoardDAO();			//free DAO
		DataBoardDAO dataDao = new DataBoardDAO();			//data DAO
		PhotoBoardDAO photoDao = new PhotoBoardDAO();		//photo DAO
		
		//******************************필요테이터취득
		noticeCount = noticeDao.getTotalRows();
		qnaCount = qnaDao.getTotalRows();
		freeCount = freeDao.getTotalRows();
		dataCount = dataDao.getTotalRows();
		photoCount = photoDao.getTotalRows();
		
		allBoardCount = noticeCount + qnaCount + freeCount + dataCount + photoCount;
		
		//===============================================================================
		adminform.setAllBoardCount(""+allBoardCount);
		adminform.setDataCount(""+dataCount +"("+getPercent(dataCount, allBoardCount)+"%)");
		adminform.setFreeCount(""+freeCount +"("+getPercent(freeCount, allBoardCount)+"%)");
		adminform.setNoticeCount(""+noticeCount +"("+getPercent(noticeCount, allBoardCount)+"%)");
		adminform.setPhotoCount(""+photoCount +"("+getPercent(photoCount, allBoardCount)+"%)");
		adminform.setQnaCount(""+qnaCount +"("+getPercent(qnaCount, allBoardCount)+"%)");
		//******************************완료
		return map.findForward("form");
	}
	
	
	public Long getPercent(long tagetVal , long totalVal)
	{
		double per ;
		long resultPercent = 0;
		
		if(tagetVal != 0)
		{
			Double tagetValD = new Double(tagetVal);
			Double totalValD = new Double(totalVal);
			
			Double result = (tagetValD/totalValD)*100;
			
			//소스점을 없애버린다.
			resultPercent = Math.round(Math.ceil(result));
		}
		return resultPercent;
	}

}
